package com.caifeng.goodstwo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片以二进制存储
 * checkbox
 */
public class MyHelp extends SQLiteOpenHelper {
    String TABLE_GOODS = "goodstable";
//    String ssss = "one";
//    String sss2 = "two";
    SQLiteDatabase mSQLiteDatabase;

    public MyHelp(Context context) {
        super(context, "goods.db", null, 1);
        mSQLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table " + TABLE_GOODS + " " +
                "(id integer primary key autoincrement, name text , number integer , price real)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    //添加数据
    public boolean insertData(String id,String name, String num, double price) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", id);
        contentValues.put("name", name);
        contentValues.put("number", num);
        contentValues.put("price", price);
        return mSQLiteDatabase.insert(TABLE_GOODS, null, contentValues) > 0;
    }

    //修改数据
    public boolean updateData(String id,String s) {
        int number;
        ContentValues contentValues = new ContentValues();
        if (s.equals("add")){
             number = getNum(id) + 1;
            contentValues.put("number", number);
        }else{
            number=getNum(id)-1;
            contentValues.put("number",number);
        }
        String sql = "id=?";
        String[] strings = new String[]{id};
        return mSQLiteDatabase.update(TABLE_GOODS, contentValues, sql, strings) > 0;

    }


    /**
     * 通过id 获取指定商品的数目
     * @param id
     * @return
     */
    public int getNum(String id) {
        SQLiteDatabase db = getReadableDatabase();
        String sql = "id=?";
        String[] strings = new String[]{id};
        Cursor cursor = db.query(TABLE_GOODS, null, sql, strings, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int number = cursor.getInt(cursor.getColumnIndex("number"));
                return number;
            }
        }
        return 0;
    }
    //删除商品
    public boolean deleteGoods(String id){
        String sql="id=?";
        String[] contentValuesArray=new String[]{String.valueOf(id)};
        return mSQLiteDatabase.delete(TABLE_GOODS,sql,contentValuesArray)>0;
    }

    public List<Goods> query() {
        List<Goods> list = new ArrayList<>();
        Cursor cursor = mSQLiteDatabase.query(TABLE_GOODS, null, null,
                null, "name", null, "name");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Goods info = new Goods();
                String id = String.valueOf(cursor.getInt(cursor.getColumnIndex("id")));//id
                String name = cursor.getString(cursor.getColumnIndex("name"));//名称
                String num = String.valueOf(cursor.getInt(cursor.getColumnIndex("number")));//数目
                String price = String.valueOf(cursor.getDouble(cursor.getColumnIndex("price")));//价格
                info.setId(id);
                //info.setNum(String.valueOf(i));

                //info.setNum(String.valueOf(count));
                info.setNum(num);
                info.setName(name);
                info.setPrice("￥" + price);
                list.add(info);
            }
            cursor.close();
        }

        return list;
    }

    /**
     * 总价值
     */
    public double getSum() {
        Cursor cursor = mSQLiteDatabase.query(TABLE_GOODS, null, null,
                null, "name", null, "name");
        double sum=0;
           while( cursor.moveToNext()){
            sum+=cursor.getDouble(cursor.getColumnIndex("price"))*cursor.getInt(cursor.getColumnIndex("number"));
            Log.e(TABLE_GOODS,String.valueOf(cursor.getDouble(cursor.getColumnIndex("price"))));
        }
        cursor.close();
        return sum;
    }

}
